package com.zork.data.generator.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.StopWatch;
import com.zork.data.generator.config.MockDataConfig;
import com.zork.data.generator.model.NodeRelationInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xiesen
 */
@Service
@Slf4j
public class ScheduledService {
    @Resource
    DataMapService dataMapService;

    @Resource
    MockData mockData;

    @Resource
    MockDataConfig mockDataConfig;

    @Resource
    ArchService archService;

    @Scheduled(fixedDelay = 600000)
    public void scheduledTasks() {
        try {
            StopWatch sw = new StopWatch();
            sw.start();
            log.info("数据资产定时更新数据地图任务开始: {}", DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"));
            final int i = dataMapService.syncDataMap();
            sw.stop();
            log.info("数据资产定时更新数据地图任务结束: {},总耗时: {}ms,一共同步了 {} 个系统.",
                    DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"),
                    sw.getTotalTimeMillis(), i);
        } catch (Exception e) {
            log.error("数据资产定时更新数据地图任务异常!", e);
        }
    }

    //    @Scheduled(fixedDelay = 1000 * 3600 * 12)
    @Scheduled(cron = "${scheduled.nodeRelation.cron:0 0/5 * * * ?}")
    public void nodeRelation() {
        try {
            StopWatch sw = new StopWatch();
            sw.start();
            log.info("数据资产定时更新统一架构节点与节点之间的映射关系任务开始: {}", DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"));
            final List<NodeRelationInfo> list = archService.nodeRelation();
            sw.stop();
            log.info("数据资产定时更新统一架构节点与节点之间的映射关系任务结束: {},总耗时: {}ms,一共同步了 {} 个系统.",
                    DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"),
                    sw.getTotalTimeMillis(), list.size());
        } catch (Exception e) {
            log.error("数据资产定时更新统一架构节点与节点之间的映射关系任务异常!", e);
        }
    }

    @Scheduled(fixedDelay = 2000)
    public void mockData() {
        try {
            if (mockDataConfig.getEnable()) {
                StopWatch sw = new StopWatch();
                sw.start();
                log.info("数据资产定时模拟数据任务开始: {}", DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"));
                mockData.log();
                mockData.alarm();
                sw.stop();
                log.info("数据资产定时模拟数据任务结束: {},总耗时: {}ms.",
                        DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"),
                        sw.getTotalTimeMillis());
            }
        } catch (Exception e) {
            log.error("数据资产定时模拟数据任务异常!", e);
        }
    }

    @Scheduled(fixedDelay = 10000)
    public void mockMetricData() {
        try {
            if (mockDataConfig.getEnable()) {
                StopWatch sw = new StopWatch();
                sw.start();
                log.info("数据资产定时模拟指标数据任务开始: {}", DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"));
                mockData.metric();
                sw.stop();
                log.info("数据资产定时模拟指标数据任务结束: {},总耗时: {}ms.",
                        DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"),
                        sw.getTotalTimeMillis());
            }
        } catch (Exception e) {
            log.error("数据资产定时模拟指标数据任务异常!", e);
        }
    }

    @Scheduled(fixedDelay = 30000)
    public void mockNoDataMonitorData() {
        try {
            if (mockDataConfig.getEnableNoData()) {
                StopWatch sw = new StopWatch();
                sw.start();
                log.info("无数据告警定时模拟指标数据任务开始: {}", DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"));
                mockData.noDataMetric();
                sw.stop();
                log.info("无数据告警定时模拟指标数据任务结束: {},总耗时: {}ms.",
                        DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"),
                        sw.getTotalTimeMillis());
            }
        } catch (Exception e) {
            log.error("无数据告警定时模拟指标数据任务异常!", e);
        }
    }
}
